package com.suzhiliang.springbootlesson.springDataJPAToCRUD;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zlsu
 *   使用SpringBoot访问MySQL数据库，
 *   并且结合SpringDataJPA完成CRUD（Create,Read,Update,Delete）简单操作.
 *
 */
@RestController
@RequestMapping("/springDataJPAToCRUD")
public class SpringDataJPAToCRUD {
    @Autowired
    private UserJPA userJPA;

    /**
     * 获取列表
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public List<UserEntity> list(){
         return userJPA.findAll();
    }

    /**
     * 保存数据或更新数据
     * @param entity
     */
    @RequestMapping(value = "save",method = RequestMethod.GET)
    public void save(UserEntity entity){
        userJPA.save(entity);
    }

    /**
     * 删除数据并获取列表
     * @param id
     */
    @RequestMapping(value = "detlete",method = RequestMethod.GET)
    public List<UserEntity> save(Long id){
        userJPA.deleteById(id);
        return userJPA.findAll();
    }


}
